# coding=utf-8
from abc import ABCMeta
import tornado.web
import tornado.escape
import json

from CommonLib import CheckJsonKeys, CheckAuth
from database.models.DatabaseCommon import *
from database.models.Normal import Normal
import WrapCursor


class InsNormalFlowHandler(tornado.web.RequestHandler):
    __metaclass__ = ABCMeta

    def post(self):
        ojson = None
        r = WrapCursor.WrapCursor()
        id = None
        try:
            ojson = json.loads(self.request.body)
            name = ojson['name']
            desc = ojson['desc']
            CheckJsonKeys(ojson, 'username', 'userpass')
            CheckAuth(ojson)
        except:
            self.write(r.Error(u'请求数据有误'))
            return
        sql = None
        ret = ""
        data_set = None
        col_name = None

        s = Session()
        sql = """
            insert into  NormalStep (Name,Desc) values (?,?)
        """
        normal = Normal(name=ojson['name'], desc=ojson['desc'])
        s.add(normal)
        try:
            s.commit()
        except:
            self.write(r.Error(u'失败:模式已存在'))
            return
        self.write(r.Ok(u'成功'))
